package org.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


/*This class add student details i.e 
 * When a user creates a profile this class is called
 * and helps user in making account in our application
 * */
public class AddStudentDetailsActivity extends Activity {
	private TextView textView1, textView2, textView3, textView4;
	private EditText editText3;
	@SuppressWarnings("unused")
	private String Title, Title_long, Author, ID, Publisher, isbn,
			Student_Name, Student_Email, Student_Phone, Book_Price,
			Course_Number;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addstudentdetailslayout);

		Bundle extras = getIntent().getExtras();

		if (extras == null) {
			return;
		}
		@SuppressWarnings("unused")
		String plyr = (String) extras.getSerializable("Value1");
		Title = (String) extras.getSerializable("Title");
		Title_long = (String) extras.getSerializable("Title_long");
		Author = (String) extras.getSerializable("Author1");
		Publisher = (String) extras.getSerializable("Publisher");
		isbn = (String) extras.getSerializable("isbn");
		ID = (String) extras.getSerializable("ID");


		Toast.makeText(this,
				"Book Received!!!! " + "\n" + "Enter your Details",
				Toast.LENGTH_LONG).show();

		editText3 = (EditText) findViewById(R.id.price);


		textView1 = (TextView) findViewById(R.id.Title);
		textView2 = (TextView) findViewById(R.id.Author);
		textView3 = (TextView) findViewById(R.id.Publisher);
		textView4 = (TextView) findViewById(R.id.ISBN);

		textView1.setText("Title: " + Title);
		textView2.setText("Author: " + Author);
		textView3.setText("Publisher: " + Publisher);
		textView4.setText("ISBN: " + isbn);


		String result = "";
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("isbn", isbn));
		
		//Given below is the working done for interacting with the remote
		//phpmyadmin database.
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://hci.cs.sfsu.edu/~fall2010.03/Includes/GetCourseNumberFromISBN.php");
		try {
			httppost.setEntity(new UrlEncodedFormEntity(postParameters));

			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();


			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();

			result = sb.toString();
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}
		
		//to fetch books for particular course number
		
		try {
			JSONArray jArray = new JSONArray(result);

			for (int i = 0; i < jArray.length(); i++) {
				JSONObject json_data = jArray.getJSONObject(i);
				Log.i("log_tag", " name: "
						+ json_data.getString("Course_Number"));

				Course_Number = json_data.getString("Course_Number");
			
			}
		} catch (JSONException e) { 
									
			System.out.println("NO RECORDS FOUND for isbn");
		}
	}

	// the back button functionality
	public void back(View view) throws Exception {
		// An intent is used to pass values to other class from existing class
		Intent i = new Intent(this, UserLogedInPageActivity.class);
		i.putExtra("ID", ID);
		startActivity(i);
	}
	
	//the method for Logout functionality
	public void logout(View view) throws Exception {

		Intent i = new Intent(this, NewOrExisitingUserActivity.class);
		startActivity(i);
	}
	
	//the onClick method for adding all the values of book into the database
	public void onClick1(View view) throws ClientProtocolException, IOException {

		
		Book_Price = editText3.getText().toString();
	
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("isbn", isbn));
		postParameters.add(new BasicNameValuePair("Title", Title));
		postParameters.add(new BasicNameValuePair("Author", Author));
		postParameters.add(new BasicNameValuePair("Publisher", Publisher));
		postParameters
				.add(new BasicNameValuePair("Student_Name", Student_Name));
		postParameters.add(new BasicNameValuePair("Student_Email",
				Student_Email));
		postParameters.add(new BasicNameValuePair("Student_Phone",
				Student_Phone));
		postParameters.add(new BasicNameValuePair("Course_Number",
				Course_Number));
		postParameters.add(new BasicNameValuePair("Book_Price", Book_Price));
		postParameters.add(new BasicNameValuePair("ID", ID));

		System.out.println("is exception here ?");

		//Interacting with the remote database using HTTP request
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://hci.cs.sfsu.edu/~fall2010.03/Includes/AddBookDetailsAfterScan.php");
		httppost.setEntity(new UrlEncodedFormEntity(postParameters));
		HttpResponse response = httpclient.execute(httppost);
		HttpEntity entity = response.getEntity();
		@SuppressWarnings("unused")
		InputStream is = entity.getContent();

		Toast.makeText(this, "Details Added!!!! ", Toast.LENGTH_LONG).show();

		Intent i = new Intent(this, UserLogedInPageActivity.class);
		i.putExtra("ID", ID);
		startActivity(i);

	}

	//method for displaying books on the screen
	public void DisplayBooks(Cursor c) {
		Toast.makeText(
				this,
				"id: " + c.getString(0)
						+ "\n"
						+ "ISBN: "
						+ c.getString(1)
						+ "\n"
						+ // ///
						"TITLE: " + c.getString(2) + "\n" + "PUBLISHER:  "
						+ c.getString(3) + "\n" + "AUTHOR:  " + c.getString(4)
						+ "\n" + "STUDENT NAME:  " + c.getString(5) + "\n"
						+ "STUDENT EMAIL:  " + c.getString(6) + "\n"
						+ "BOOK PRICE:  " + c.getString(7), Toast.LENGTH_LONG)
				.show();

	}
}